<?php
/**
 * a third example
 * 
 * needs write access to kata-root-folder for mocking
 * 
 * @package katatest_case
 */


/**
 * include required files (really? :O)
 */
require_once 'lib'.DIRECTORY_SEPARATOR.'kataTestBase.simpleTest.class.php';
require_once BASEPATH.'lib/controller.php';
require_once BASEPATH.'lib/controllers/app_controller.php';


/**
 * 
 * @author Dietmar Riess
 * @package katatest_case
 */
class Example3Test  extends kataTestBaseClass {
	private $myModel ;
	
	function testInitialize() {
		$this->bootstrapKata();
	}
	
	function testInitializeViaDispatcher(){
		//ajax Request
		//ajax test, braucht kein default layout
			Mock::generatePartial('AppController','MockController',array('beforeRender','beforeAction','beforeFilter','afterFilter','index'));
			$dispatcher=new dispatcher();
			$this->createTmpFile("views".DS.'mock'.DS.'index.thtml','<?php //generated by Test?>Hallo Welt');
			$this->createTmpFile("controllers".DS.'mock_controller.php',"<?php //generated by Test ?>");
			$return = $dispatcher->dispatch('ajax/Mock/index');
			$this->assertContain("Hallo Welt",$return);
			$this->assertEqual($dispatcher->controller->autoRender,false);
			$dispatcher->controller->expectCallCount("index",1);
			$dispatcher->controller->expectCallCount("beforeAction",1);
			$dispatcher->controller->expectCallCount("afterFilter",1);
			$dispatcher->controller->expectCallCount("beforeFilter",1);
	    //Request
	    //default Page ist index...
	    	$dispatcher=new dispatcher();
			$this->createTmpFile("views".DS.'layouts'.DS.'default.thtml','<?php //generated by Test?><?php echo $content_for_layout; ?>');
			$return = $dispatcher->dispatch('Mock/index');
			$this->assertNotContain("404 Not Found",$return);
			$this->assertContain("Hallo Welt",$return);
			$this->assertEqual($dispatcher->controller->autoRender,false);
			$dispatcher->controller->expectCallCount("index",1);
			$dispatcher->controller->expectCallCount("beforeAction",1);
			$dispatcher->controller->expectCallCount("afterFilter",1);
			$dispatcher->controller->expectCallCount("beforeFilter",1);
		//Request mit mehreren RenderVersuchen
			$dispatcher=new dispatcher();
			$this->createTmpFile("controllers".DS.'mock2_controller.php',"<?php //generated by Test ?>");
			$this->createTmpFile("views".DS.'mock2'.DS.'index.thtml',"<?php //generated by Test// ?>Hallo Welt");
			$this->createTmpFile("views".DS.'mock2'.DS.'index2.thtml',"<?php //generated by Test// ?>AngryBeaver");
			$return = $dispatcher->dispatch('Mock2/index');
			$this->assertNotContain("404 Not Found",$return);
			$this->assertContain("AngryBeaver",$return);
	}

	function testInitializeViaDispatcher2(){
		//controller File ist App_controller
			$dispatcher=new dispatcher();
			ob_start();
			$dispatcher->dispatch('App');
			$this->assertContain("404 Not Found",ob_get_clean());
		//controller File ist nicht da
			$dispatcher=new dispatcher();
			ob_start();
			$dispatcher->dispatch('Mock');
			$return = ob_get_clean();
			$this->assertContain("404 Not Found",$return);
	}
	
	function testInitializeException(){
		Mock::generatePartial('Controller','Mockoller',array('get'));
		$myMockController = &new Mockoller();
		$this->expectException("Exception");
		$myMockController->__construct();
	}
	
	function testGetSet(){
		$myController = new AppController;
		$myController->set("name","AngryBeaver");
		$myController->set("title","Meck");
		$myController->set("get",array("noch"=>"mehr"));
		$return = $myController->get("name");
		$this->assertEqual($return,"AngryBeaver");
		$return = $myController->get("title");
		$this->assertEqual($return,"Meck");
		$return = $myController->get("get");
		$this->assertEqual($return,array("noch"=>"mehr"));
	}
	
	function testRender(){
		Mock::generatePartial('AppController','MockRenderViewController',array('renderView'));
		$myMockController = &new MockRenderViewController();
		$myMockController->setReturnValue('renderView',"defaultLayout",array("index",false));
		$myMockController->setReturnValueAt(1,'renderView',"layout",array("index","xyz"));
		//ruft renderView
			$myMockController->render("index");
			$myMockController->expectCallCount("renderView",2);
			$this->assertEqual($myMockController->output,"defaultLayout");
		//übergibt an renderView die richtigen parameter
			$myMockController->render("index","xyz");
			$this->assertEqual($myMockController->output,"layout");
		//Render wird in output variable gespeichert
			$myMockController = &new MockController();
			$this->createTmpFile("views".DS.'mock'.DS.'index.thtml','<?php //generated by Test?>Hallo Welt');
			$this->createTmpFile("views".DS.'layouts'.DS.'default.thtml','<?php //generated by Test?><?php echo $content_for_layout; ?>');
			$myMockController->render("index");
			$this->assertEqual($myMockController->output,"Hallo Welt");
	}
	
	function testRenderView(){
		$myMockController = &new MockController();
		$this->createTmpFile("views".DS.'mock'.DS.'index.thtml',"<?php //generated by Test// ?>Hallo Welt");
	    $return = $myMockController->renderView("index",null);
	    //renderView ruft after und beforeFilter auf
	   		$myMockController->expectCallCount("afterFilter",1);
			$myMockController->expectCallCount("beforeFilter",1);
		//setzt den autorenderer auf false;
			$this->assertEqual($myMockController->autoRender,false);
		//hat als rückgabe Parameter die View (korrekt gerendert)
			$this->assertContain("Hallo Welt",$return);
			$this->assertNotContain("<?",$return);
			$this->assertNotContain("Fatal error:",$return);
			$this->assertNotContain("404 Not Found",$return);
		//die gerenderte View hat die übergebenen Parameter (korrekt gerendert)
			$myMockController = &new MockController();
			$this->createTmpFile("views".DS.'mock'.DS.'index.thtml','<?php //generated by Test// ?>xyz<?=$name?>');
			$myMockController->set("name","AngryBeaver");
			$return = $myMockController->get("name");
			$this->assertEqual($return,"AngryBeaver");
			$return = $myMockController->renderView("index",null);
			$this->assertContain("AngryBeaver",$return);
			$this->assertNotContain("<?",$return);
			$this->assertNotContain("Fatal error:",$return);
			$this->assertNotContain("404 Not Found",$return);
		//die gerenderte View kann auch mit arrays als Parameter umgehen
			$myMockController = &new MockController();
			$myMockController->set("name",array("AngryBeaver"));
			$this->createTmpFile("views".DS.'mock'.DS.'index.thtml','<?php //generated by Test// ?><?=$name[0]?>');
			$return = $myMockController->renderView("index");
			$this->assertEqual("AngryBeaver",$return);
			$myMockController->expectCallCount("afterFilter",1);
			$myMockController->expectCallCount("beforeFilter",1);
	}

	function testRenderCachedHtml(){
			$myMockController = &new MockController();
			$myMockController->set("name","AngryBeaver");
			$return = $myMockController->renderCachedHtml("AngryBeaver",null);
		//$this->assertEqual("AngryBeaver",$myMockController->output);
		//gerenderte View ist rückgabewert
			$this->assertEqual("AngryBeaver",$return);
		//before und after Filter wurden nicht ausgeführt
			$myMockController->expectCallCount("afterFilter",0);
			$myMockController->expectCallCount("beforeFilter",0);
	}
}


/**
 * 
 * @author Dietmar Riess
 * @package katatest_case
 */
class Mock2Controller extends AppController{
	function mockRedirect(){
		$path = "";
		foreach (func_get_args() as $arg) {
			if(!empty($path)){
				$path = $path.'/';
			}
			$path=$path.$arg;
		}
		$this->redirect($path);
	}
	function index(){
		$this->render("index2");
	}
}
